<template>
  <div class="status">
    <!-- 搜索框 -->
    <searchVue></searchVue>
    <!-- 歌曲切换条 -->
    <switchVue></switchVue>
    <!-- 歌曲状态栏 -->
    <songStatus></songStatus>
    <div class="userBar">
      <drawer v-show="portraitFlag"></drawer>
      <!-- 头像 -->
      <img v-show="portraitFlag" :src="$store.state.userMsg.avatar_url" alt="" @click="drawer = !portraitFlag, goUser()">
      <span v-show="!portraitFlag" @click="drawer = !portraitFlag" class="spanL">亲爱的，请登录</span>
      <span v-show="portraitFlag" @click="drawer = !portraitFlag" class="spanL">{{ $store.state.userMsg.user_name }}</span>
      <el-dropdown v-show="portraitFlag" trigger="click" @command="drop()">
        <span class="el-dropdown-link">
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-drawer style="overflow:hidden" title="我是标题" size="90%" ref="editDrawer" :closeDrawer="closeDrawer"
      :with-header="false" :append-to-body="true" :modal-append-to-body="true" :visible.sync="drawer"
      :direction="direction" :before-close="handleClose">
      <div class="register">
        <el-button type="primary" circle class="reg" @click="toRegister" v-if="flag">
          <span>去注册 ></span>
        </el-button>
        <el-button type="primary" circle class="reg" @click="toRegister" v-else>
          <span>去登录 ></span>
        </el-button>
      </div>
      <login @enter="toHome" style="overflow:hidden" v-if="flag"></login>
      <register @login="toLogin" v-else></register>
    </el-drawer>
  </div>
</template>

<script>
import login from "@/views/login-register/login.vue"
import register from "@/views/login-register/Entrance.vue"
import searchVue from '@/components/search.vue'
import switchVue from '@/components/switch.vue'
import songStatus from '@/components/songStatus.vue'
import drawer from "@/components/drawer/drawer.vue"
import { url } from "@/apiUrl"//引入地址
export default {
  components: {
    searchVue,
    switchVue,
    songStatus,
    drawer,
    login,
    register,
    searchVue
  },
  data() {
    return {
      drawer: false,
      direction: 'btt',
      flag: true,
      portraitFlag: false,
      uname: ''
    }
  },
  methods: {
    handleClose(done) {//打开播放列表
      done();
    },
    closeDrawer(done) {//关闭播放列表
      done();
    },
    toRegister() {//登录和注册的切换
      this.$data.flag = !this.$data.flag
    }, 
    toLogin(data) {//切换到登录页
      setTimeout(() => {
        this.$data.flag = data
      }, 1.5 * 1000)
    },
    toHome(data, uname) {//用户登录成功，跳转主页
      console.log(uname);
      this.$data.uname = this.$store.state.userMsg.user_name;
      this.$data.portraitFlag = data;
      this.$refs.editDrawer.closeDrawer(() => { })
      this.$axios
        .post(`${url}/users/getUserData`,{
          t:localStorage["qtUser"]
        },{
    headers:{
      'Authorization': localStorage['qtUser']
    }
})
        .then((res) => {
          if (typeof res.data.data != "undefined"){
            console.log(res.data.data);
            this.$store.commit("getUsermsg",res.data.data.userMsg[0])
          }
        })
    },
    goUser() {//跳转用户页
      this.$router.push("userPage").catch(() => { })
    },
    drop() {//退出登录
      this.$data.portraitFlag = false;
      delete localStorage["qtUser"];//移除localstorage中的token
      console.log("移除后的localstorage：" + localStorage["qtUser"]);
      this.$store.commit("drop")
      this.$message("您已退出")
      this.$router.replace("musicHall").catch(() => { })
    }
  },
  mounted() {
    //判断用户有没有登录
    if (localStorage.getItem('qtUser')) {
      //判断token有没有过期
      this.$axios
        .post(`${url}/users/getUserData`,{
          t:localStorage["qtUser"]
        },{
    headers:{
      'Authorization': localStorage['qtUser']
    }
})
        .then((res) => {
          if (typeof res.data.data != "undefined") {
            console.log("这是成功返回的数据", res.data.data)
            this.toHome(true, localStorage.getItem('qtUserName'))
            this.$store.commit("getUsermsg",res.data.data.userMsg[0])
          }else{
            delete localStorage["qtUser"];//移除localstorage中的token
            this.$message("您的账户已超时,请重新登录")
          }
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../assets/common.scss';
.spanL{
  font-size: 13px;
}
.songStatus {
  width: 55rem;
  margin: 0 -12rem 0 -3rem;
  
}

.userBar {
  display: flex;
  align-items: center;
}

.reg {
  width: 30rem;
  height: 30rem;
  margin-top: -15.5rem;
  margin-left: -13.1rem;
  z-index: 0;

  span {
    font-size: 24px;
    font-weight: bold;
    display: inline-block;
    margin: 10rem 0 0 8rem;
  }
}

.register {
  z-index: 0;
  display: inline;
  width: 50rem;
  overflow: hidden;
}

span {
  margin-left: 0.5rem;
}

.status {
  overflow: hidden;
  z-index: 3;
  user-select: none;
  background: $undercolor;
  display: flex;
  justify-content: space-around;
  align-items: center;
  position: sticky;
  top: 0;
  margin-left: $leftNavMargin - 0.1rem;
  box-shadow: rgba(0, 0, 0, 0.054) 0px -23px 25px 0px inset, 
  rgba(0, 0, 0, 0.057) 0px -36px 30px 0px inset, 
  rgba(0, 0, 0, 0.065) 0px -79px 40px 0px inset, 
  rgba(0, 0, 0, 0.057) 0px 2px 1px,
   rgba(0, 0, 0, 0.056) 0px 4px 2px, 
   rgba(0, 0, 0, 0.068) 0px 8px 4px,
   rgba(0, 0, 0, 0.06) 0px 16px 8px, 
   rgba(0, 0, 0, 0.063) 0px 32px 16px;
    img {
    cursor: pointer;
    padding: 7px 0;
    width: 36px;
    border-radius: 50%;
  }

  div {
    // display: flex;
  }
}
</style>